﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace ChuongtrinhTracnghiem
{
    public partial class FormKetqua : Form
    {
        public FormKetqua()
        {
            InitializeComponent();
        }

        private void FormKetqua_Load(object sender, EventArgs e)
        {
            int lan;
            TreeNode level0, level1;
            string s = "select * from TblUser";
            DataTable ds = new DataTable();
            Model.Laydulieu dl = new Model.Laydulieu();
            ds = dl.Dulieu(s);
            Model.Ketnoi.Boketnoi();
            foreach (DataRow r in ds.Rows)
            {
                level0 = treeView1.Nodes.Add(r["Hoten"].ToString());
                level0.Tag = r["UserID"].ToString();
                level0.ForeColor = Color.Blue;

                string s1 = "select * from TblKiemtra where UserID = '" + level0.Tag + "'";
                DataTable ds1 = new DataTable();
                Model.Laydulieu dl1 = new Model.Laydulieu();
                ds1 = dl1.Dulieu(s1);
                Model.Ketnoi.Boketnoi();
                lan = 1;
                foreach (DataRow r1 in ds1.Rows)
                {
                    level1 = level0.Nodes.Add("Lần Test thứ "+lan++);
                    level1.Tag = r1["ID"].ToString();
                    level1.ForeColor = Color.Green;
                    level1.ToolTipText = r1["ID"].ToString();
                }
            }
            treeView1.ExpandAll();

        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                string s = "SELECT  TblCauhoi.CauHoi, TblCauhoi.Caudung, TblChitietKiemtra.Traloi, TblKiemtra.Socau,TblKiemtra.Traloidung,TblKiemtra.ID, TblUser.UserID";
                s = s + " FROM TblChitietKiemtra INNER JOIN";
                s = s + " TblKiemtra ON TblChitietKiemtra.ID = TblKiemtra.ID INNER JOIN";
                s = s + " TblUser ON TblKiemtra.UserID = TblUser.UserID INNER JOIN";
                s = s + " TblCauhoi ON TblChitietKiemtra.CauhoiID = TblCauhoi.CauHoiID";
                s = s + " WHERE TblKiemtra.ID in (select TblKiemtra.ID from TblUser, TblKiemtra where  TblKiemtra.ID = TblUser.UserID and TblUser.Hoten = '" + e.Node.Parent.ToString() + "')";

                DataTable ds = new DataTable();
                Model.Laydulieu dl = new Model.Laydulieu();
                ds = dl.Dulieu(s);
                dataGridView1.DataSource = ds;

                dataGridView1.Columns[0].HeaderText = "Câu hỏi";
                dataGridView1.Columns[1].HeaderText = "Đáp án";
                dataGridView1.Columns[2].HeaderText = "Trả lời";

                dataGridView1.Columns[0].Width = 300;
                dataGridView1.Columns[1].Width = 70;
                dataGridView1.Columns[2].Width = 70;

                dataGridView1.Columns[3].Visible = false;
                dataGridView1.Columns[4].Visible = false;
                dataGridView1.Columns[5].Visible = false;
                dataGridView1.Columns[6].Visible = false;

                label_KQ.Text = "Kết quả bài Test : " + ds.Rows[0]["Traloidung"] + " câu đúng/" + ds.Rows[0]["Socau"]+" câu";
            }
            catch { label_KQ.Text = ""; }
            Model.Ketnoi.Boketnoi();
            
        }
    }
}
